const db = require('../dao/database')

/**
 * 用户登录
 * @param username
 * @param password
 * 
 * @returns isExited
*/
let checkUser = async function (username, password) {
    let sql = `select * from user where username='${username}' and password='${password}'`
    try {
        let res = await db(sql, [])
        return res.length > 0
    } catch (error) {
        console.log(error);
        return false
    }
}

/**
 * 查找用户信息
 * 
 * **/

let getUserInfo = async function (username) {
    let sql = `select username, type, telephone from user where username = '${username}'`

    try {
        let res = await db(sql, [])
        return res
    } catch (error) {
        console.log(error);
        return false
    }

}
/**
 *用户注册
 * @param username 
 * @param password
 * @param telephone
 * @param type
 * 
*/
let regist = async function (username, password, telephone, type) {

    let sql = `insert into user (username, password, telephone, type) values ('${username}', '${password}', '${telephone}', '${type}')`
    try {
        let res = await db(sql, [])
        if (res.affectedRows === 1) {
            return true
        } else {
            return false
        }
    } catch (error) {
        console.log(error);
        return false
    }
}

/**
 * 更新用户信息
 * **/
let updataUser = async function (username, password, telephone, type) {
    let sql = `UPDATE USER SET PASSWORD = '${password}', telephone='${telephone}',TYPE='${type}' WHERE username = '${username}'`
    try {
        let res = await db(sql, [])
        if (res.affectedRows === 1) {
            return true
        } else {
            return false
        }
    } catch (error) {
        console.log(error);
        return false
    }

}

/**
 * 用户退出
 * */ 

let loginout = async function (username) {
    return true
}

module.exports = {
    checkUser,
    getUserInfo,
    regist,
    updataUser,
    loginout
}